package org.jeecg.modules.wlhy.fanyun.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.daikai.fanyun.dto.FyApiBaseResDTO;
import org.jeecg.modules.third.daikai.fanyun.dto.vehicle.FyVehicleDTO;
import org.jeecg.modules.third.daikai.fanyun.service.FanyunApiService;
import org.jeecg.modules.third.daikai.mabang.dto.MbVehicleDTO;
import org.jeecg.modules.wlhy.fanyun.entity.HyFyVehicle;
import org.jeecg.modules.wlhy.fanyun.mapper.HyFyVehicleMapper;
import org.jeecg.modules.wlhy.fanyun.service.IHyFyVehicleService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: hy_fy_vehicle
 * @Author: duanlikao
 * @Date:   2023-02-08
 * @Version: V1.0
 */
@Service
public class HyFyVehicleServiceImpl extends ServiceImpl<HyFyVehicleMapper, HyFyVehicle> implements IHyFyVehicleService {


    @Resource
    private FanyunApiService fanyunApiService ;

    @Resource
    private IHyVehicleService hyVehicleService;


    @Override
    public Result uploadVehicle(String vehicleId, String domianURL) {
        HyVehicle vehicle = hyVehicleService.getById(vehicleId);
        if (vehicle == null) {
            throw new JeecgBootException("该车辆上报信息不能为空");
        }

        HyFyVehicle hyFyVehicle = new HyFyVehicle();
        hyFyVehicle.setVehicleId(vehicleId);
        hyFyVehicle.setUploadTime(new Date());

        FyVehicleDTO paramDTO = this.vehicleTOFyVehicleDTO(vehicle, domianURL);
        String res = fanyunApiService.vehiclePush(paramDTO);
        if (StrUtil.isEmpty(res)) {
            hyFyVehicle.setUploadStatus("2");
            hyFyVehicle.setUploadFailureReason("推送异常");
            this.saveOrUpdateFyVehicle(hyFyVehicle);
            return Result.error("该车辆数据推送失败");
        }

        FyApiBaseResDTO r = JSONObject.parseObject(res, FyApiBaseResDTO.class);
        if (CommonConstant.SC_OK_200.intValue() != r.getCode()) {
            if (r.getMessage().contains("已存在")) {
                hyFyVehicle.setUploadStatus("1");
                this.saveOrUpdateFyVehicle(hyFyVehicle);
                return Result.OK();
            } else {
                hyFyVehicle.setUploadStatus("2");
                hyFyVehicle.setUploadFailureReason(r.getMessage());
                this.saveOrUpdateFyVehicle(hyFyVehicle);
                return Result.error("推送失败,原因: 车辆 " + vehicle.getVehicleNumber() + r.getMessage());
            }
        }

        hyFyVehicle.setUploadStatus("1");
        this.saveOrUpdateFyVehicle(hyFyVehicle);
        return Result.OK();
    }


    public FyVehicleDTO vehicleTOFyVehicleDTO(HyVehicle vehicle ,String domianURL) {
        if (vehicle == null) {
            return null;
        }
        FyVehicleDTO fyVehicleDTO = new FyVehicleDTO() ;

        fyVehicleDTO.setLicensePlateNumber(vehicle.getVehicleNumber()) ;
        fyVehicleDTO.setVehicleOwner(vehicle.getOwner()) ;
        fyVehicleDTO.setIssuingAuthority(vehicle.getIssuingOrganizations()) ;
        fyVehicleDTO.setMerchantName(vehicle.getOwner()) ;
        fyVehicleDTO.setVehicleType(vehicle.getVehicleType());
        fyVehicleDTO.setLicensePlateColorCode(vehicle.getVehiclePlateColorCode()) ;
        fyVehicleDTO.setVehicleEnergyTypeCode(vehicle.getVehicleEnergyType()) ;
        fyVehicleDTO.setVehicleIDCode(vehicle.getVin()) ;
        fyVehicleDTO.setRoadTransportLicenseNumber(vehicle.getBusinessLicense());
        fyVehicleDTO.setRoadTransportCertificate(vehicle.getRoadTransportNumber()) ;
        fyVehicleDTO.setRegistrationDate(vehicle.getRegisterDate().getTime()/1000);
        fyVehicleDTO.setIssueDate(vehicle.getIssueDate().getTime()/1000)    ;
        fyVehicleDTO.setApprovedLoadWeight(
                vehicle.getVehicleTonnage().multiply(new BigDecimal("1000")).intValue()
        ) ;
        fyVehicleDTO.setTotalWeight(
                vehicle.getGrossMass().multiply(new BigDecimal("1000")).intValue()
        )  ;
        List<String>  drivingLicenseUrlList = new ArrayList<>() ;
        drivingLicenseUrlList.add(domianURL + vehicle.getDrivingLicenseImg()) ;
        drivingLicenseUrlList.add(domianURL + vehicle.getDrivingLicenseImg2()) ;
        fyVehicleDTO.setDrivingLicenseUrlList(drivingLicenseUrlList) ;

        List<String>  roadTransportPermitUrl = new ArrayList<>() ;
        if(StringUtils.isNotBlank(vehicle.getRoadTransportImg())){
            roadTransportPermitUrl.add(domianURL + vehicle.getRoadTransportImg()) ;
        }
        if(StringUtils.isNotBlank(vehicle.getRoadTransportImg2())){
            roadTransportPermitUrl.add(domianURL + vehicle.getRoadTransportImg2()) ;
        }
        fyVehicleDTO.setRoadTransportPermitUrlList(roadTransportPermitUrl) ;

        fyVehicleDTO.setTrailerLicenseNumber(
                StringUtils.isNotBlank(vehicle.getTrailerVehiclePlateNumber())  ?
                        vehicle.getTrailerVehiclePlateNumber() : vehicle.getVehicleNumber().substring(0,6)+"挂"
        );

        return fyVehicleDTO;
    }

    @Override
    public boolean saveOrUpdateFyVehicle(HyFyVehicle hyFyVehicle){
        try {
            hyFyVehicle.setId(hyFyVehicle.getVehicleId());
            return this.saveOrUpdate(hyFyVehicle);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }
}
